//
//  YZMRechargeViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/5.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMRechargeViewController.h"
#import "YZMAlertView.h"
#import "YZMRechargeCell.h"
#import "YZGeneralResultModel.h"
#import "YZClient+RechargeDraw.h"


#define payLianlian @"pay_lianlian"
#define payLiandong @"pay_liandong"
#define payAiBei    @"pay_iapppay"
#define payZFB      @"pay_zhifubao"
#define payHeFuBao  @"pay_hfbpay"

#define FastPay  @"快捷支付"



static NSString *const sYZMRechargeCellID = @"YZMRechargeCellID";

@interface YZMRechargeViewController()

@property (nonatomic, strong) NSMutableArray *payNames;
@property (nonatomic, strong) NSMutableArray *descs;
@property (nonatomic, strong) NSDictionary *params;
@property (nonatomic, strong) NSMutableArray *payKey;

@end

@implementation YZMRechargeViewController

- (instancetype)init{
    
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"账户充值";
    }
    return self;
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    self.payNames = [NSMutableArray array];
    self.descs = [NSMutableArray array];
    self.payKey = [NSMutableArray array];
    [self requestCheckPayCount];
    [self configTableView];
}

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    if (!YZMUserServiceInstance.user.detail.isNamed) {
        
        YZMAlertView *alertView = [YZMAlertView alertViewForRecommandWithContent:@"您还未实名认证，请先认证"];
        alertView.cancelButton.hidden = NO;
        [alertView.sureButton setTitle:@"立即验证" forState:UIControlStateNormal];
        alertView.cancleHandle = ^{
          
            [YZMHCMInstance popViewController:nil animated:YES];
        };
        alertView.confirmHandle  = ^{
          
            [YZMHCMInstance pushViewControllerWithName:@"identifyVerify" andParams:nil];
            return YES;
        };
        [alertView showInView:self.navigationController.view];
        
        return;
    }
}

- (void)quickPay{
    
    [YZMHCMInstance pushViewControllerWithName:@"fastPay" andParams:self.params];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.payNames.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YZMRechargeCell *cell = [tableView dequeueReusableCellWithIdentifier: sYZMRechargeCellID forIndexPath:indexPath];
    
    NSInteger row = indexPath.section;
    if ([self.payKey[row] isEqualToString:payLianlian] || [self.payKey[row] isEqualToString:payLiandong] ||[self.payKey[row] isEqualToString:payAiBei] || [self.payKey[row] isEqualToString:payHeFuBao]) {
        if ([self.payKey[row] isEqualToString:payAiBei]) {
              cell.recommandFlagShow = NO;
        }else{
            cell.recommandFlagShow = YES;
        }
        cell.logoImageView.image = [UIImage imageNamed: @"icon_recharge_fastpay"];
    }else if([self.payKey[row] isEqualToString:payZFB]){
        cell.recommandFlagShow = NO;
        cell.logoImageView.image = [UIImage imageNamed:@"icon_recharge_payZF"];
        cell.logoImageView.layer.cornerRadius = 4;
        cell.logoImageView.layer.masksToBounds = YES;
    }
    cell.payName = self.payNames[row];
    cell.payHint = self.descs[row];
    YZMIndexCount indexCount;
    indexCount.index = 0;
    indexCount.count = 1;
    cell.indexCount = indexCount;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath: indexPath animated: YES];
    
    if ([self.payKey[indexPath.section] isEqualToString:payLianlian] ||[self.payKey[indexPath.section] isEqualToString:payLiandong] || [self.payKey[indexPath.section] isEqualToString:payAiBei] || [self.payKey[indexPath.section] isEqualToString:payHeFuBao]) {
        self.params = @{@"payMethod":self.payKey[indexPath.section]};
        [self quickPay];
        
    }else if([self.payKey[indexPath.section] isEqualToString:payZFB]){
        
        //支付宝支付
        [YZMHCMInstance pushViewControllerWithName:@"payform" andParams:self.params];
    }
}

#pragma mark -- ui

- (void)configTableView
{
    [self.tableView registerClass: [YZMRechargeCell class] forCellReuseIdentifier: sYZMRechargeCellID];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.rowHeight = 74;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *headerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
    UILabel *label = [YZMAppStyle labelWithMediumWhite];
    label.text  = @"选择充值方式";
    [headerView addSubview: label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(headerView).with.offset(15);
        make.centerY.equalTo(headerView);
    }];
    
    self.tableView.tableHeaderView = headerView;
}

#pragma mark - netWork
- (void)requestCheckPayCount{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    WS(ws);
    [YZClient checkPayMethodCountWithSuccess:^(NSURLResponse *task, id responseObject) {
        hud.hidden = YES;
        YZGeneralResultModel *model = responseObject;
        if (model.success) {
            if (model.data != nil || [model.data isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dic in model.data) {
                    if ([dic[@"pay_status"] integerValue] == 1) {
                        if ([dic[@"pay_key"] isEqualToString:payLianlian] || [dic[@"pay_key"] isEqualToString:payLiandong] || [dic[@"pay_key"] isEqualToString:payAiBei] || [dic[@"pay_key"] isEqualToString:payHeFuBao]) {
                            [ws.payKey addObject:dic[@"pay_key"]];
                            [ws.payNames addObject:dic[@"pay_desc"]];
                            [ws.descs addObject:@"快速安全，24小时支付"];
                        }else if ([dic[@"pay_key"] isEqualToString:payZFB] ){
                            [ws.payKey addObject:dic[@"pay_key"]];
                            [ws.payNames addObject:dic[@"pay_desc"]];
                            [ws.descs addObject: @"手机支付，免手续费"];
                        }
                    }
                }
            }
            if (ws.payNames.count > 0) {
                [ws.tableView reloadData];
            }
        }else{
            [MBProgressHUD showMessage:model.resultMessage];
        }
        
    } failure:^(NSError *error) {
        hud.hidden = YES;
        [error yz_showMessage];
        
    }];
}


#pragma mark - controller manager support

- (void)updateWithParams: (NSDictionary *)params
{
    self.params = [NSDictionary dictionaryWithDictionary:params];
}

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMRechargeViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams: params];
    return ctrl;
}

@end
